(* Avoir la prochaine ligne, renvoie le start, et la fin dans le pos *)
let next_ligne buffer pos nb = 
  let regexp = Str.regexp "\n" in
  try
    pos := Str.search_forward regexp buffer !pos;
    nb := !nb + 1;
  with Not_found -> pos := 0

let textSearch buffer text stat note =
    let rexp = Str.regexp text in
    let pos = ref 0 in
    let nb = ref 0 in
    let start = ref 0 in
    let posrxp = ref 0 in
    next_ligne buffer pos nb;
    Compil.public_remove_tags note;
    while (!pos != 0) do
        let buf = (String.sub buffer !start (!pos-(!start))) in
        begin
        try 
            while (true) do
                posrxp := Str.search_forward rexp buf !posrxp;
                Compil.get_color (!nb-1) !posrxp 
                    (!posrxp + String.length text) note false;
                print_endline (string_of_int !nb);
                print_endline (Str.matched_string buf);
                posrxp := !posrxp + 1;
            done
        with Not_found -> ()
        end;
        start := !pos + 1;
        pos := !pos + 1;
        posrxp := 0;
        next_ligne buffer pos nb
    done

let textSearchs buffer text start =
  let cmpt = ref 1 in
  let rexp = Str.regexp (".*\\("^text^".*\\)") in
  if (Str.string_match rexp buffer start) then
    print_endline (Str.matched_group !cmpt buffer)

let textReplace all buffer textsearch textreplace = 
  let strSearch = "\\(.*\\)"^textsearch^"\\(.*\\)" in
  let strReplace = "\\1"^textreplace^"\\2" in
  let rexp = Str.regexp_case_fold strSearch in
  if all then
    (Str.global_replace rexp strReplace buffer)
  else (Str.replace_first rexp strReplace buffer)

let addSearchReplace note =
    let current = note#current_page in
  let currentTextCode = (List.nth !Function._listText current) in
  let dialog = GWindow.dialog
      ~title:"Search & Replace"
      ~height:150
      ~width:400 () in
  let dBoxh = GPack.hbox 
      ~border_width:5
      ~spacing:5
      ~packing:dialog#vbox#pack () in
  let dBoxb = GPack.hbox 
      ~border_width:5
      ~spacing:5
      ~packing:dialog#vbox#pack () in
  let _ = GMisc.label ~text:"Rechercher"
      ~packing:dBoxh#pack () in
  let dTSearch = GEdit.entry
      ~packing:dBoxh#pack () in
  let _ = GMisc.label ~text:"Remplacer"
      ~packing:dBoxb#pack () in
  let dTReplace = GEdit.entry
      ~packing:dBoxb#pack () in
  let dBSearch = GButton.button ~label:"Rechercher" 
      ~packing:dialog#action_area#pack () in
  let dBReplace = GButton.button ~label:"Remplacer 1er" 
      ~packing:dialog#action_area#pack () in
  let dBReplaceAll = GButton.button ~label:"Remplacer tous" 
      ~packing:dialog#action_area#pack () in
  let buffer = currentTextCode#buffer#get_text () in
  let sbuffer x = currentTextCode#buffer#set_text x in
  let _ = dBSearch#connect#clicked 
      (fun () -> textSearch buffer dTSearch#text 0 note) in
  let _ = dBReplace#connect#clicked 
      (fun () -> sbuffer (textReplace false buffer dTSearch#text dTReplace#text)) in
  let _ = dBReplaceAll#connect#clicked 
      (fun () -> sbuffer (textReplace true buffer dTSearch#text dTReplace#text)) in
  let _ = dialog#run () in
  dialog#destroy ()
